<template>
  <div class="page-container">
    <div class="control-container">
      <div class="left-panel shrink-panel" :class="isWide?'':'narrow'">
        <div class="shrink-btn-panel">
          <i :class="isWide?'el-icon-arrow-left':'el-icon-arrow-right'" @click="isWide = !isWide" />
        </div>
        <OutletTree
          v-if="isWide"
          @handleNodeClick="handleNodeClick"
        />
      </div>
      <div class="right-panel overflow-x">
        <div class="search-panel">
          <el-form :inline="true" label-width="100">
            <el-form-item label="最新">
              <el-radio-group v-model="radioData" @change="changeRadio()">
                <el-radio :label="1">当天</el-radio>
                <el-radio :label="2">3天</el-radio>
                <el-radio :label="3">7天</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="时间范围">
              <el-date-picker
                v-model="time"
                type="datetimerange"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00','23:59:59']"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button type="primary" :loading="exportLoading" @click="handleExport">导出</el-button>
          </el-form>
        </div>
        <div class="table-panel">
          <el-table
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 400px)"
            :data="tableData"
          >
            <el-table-column align="center" type="index" width="80" />
            <el-table-column prop="alarmInformationObj.enterpriseName" label="企业名称" />
            <el-table-column prop="alarmInformationObj.outletName" label="排口名称" />
            <el-table-column prop="alarmType" label="报警类型">
              <template slot-scope="scope">{{ getDictLabel('e001', scope.row.alarmType) }}
              </template>
            </el-table-column>
            <el-table-column prop="alarmInformation" label="报警信息" width="200" />
            <el-table-column prop="alarmStartTime" label="报警开始时间" />
            <el-table-column prop="alarmEndTime" label="报警结束时间" />
            <el-table-column prop="duration" label="持续时长">
              <template v-if="scope.row.duration" slot-scope="scope">
                {{ scope.row.durationStr }} ( {{ scope.row.duration }} )
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="80">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="handleVerify(scope.row)">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <Pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <DetailsDialog
      :dialog-visible="dialogVisible"
      :dialog-type="dialogType"
      :obj="obj"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import OutletTree from '@/components/Tree/outletTree'
import { getList, download } from '@/api/business/historyAlarm'
import { parseTime } from '@/utils/index'
import { getDictLabel, downloadFile } from '@/utils'
import DetailsDialog from './components/detailsDialog'
import { getDatefmt } from '@/utils/common'

export default {
  name: 'HistoryAlarm',
  components: {
    Pagination, DetailsDialog, OutletTree
  },
  data() {
    return {
      isWide: true,
      query: {
        total: 0,
        page: 1,
        limit: 10,
        projectDepartmentId: null,
        enterpriseId: null,
        outletId: null
      },
      radioData: 1,
      time: [],
      tableData: [],
      tableLoading: false,
      exportLoading: false,
      dialogVisible: false,
      dialogType: 'detail',
      obj: {}
    }
  },
  created() {
    this.changeRadio()
    this.initData()
  },
  methods: {
    parseTime,
    getDictLabel,
    changeRadio() {
      this.time = []
      const date = new Date()
      this.$set(this.time, 1, getDatefmt(date, 'yyyy-MM-dd') + ' 23:59:59')
      if (this.radioData === 1) {
        this.$set(this.time, 0, getDatefmt(new Date(), 'yyyy-MM-dd') + ' 00:00:00')
      } else if (this.radioData === 2) {
        this.$set(this.time, 0, getDatefmt(new Date(new Date().setDate(date.getDate() - 3)), 'yyyy-MM-dd') + ' 00:00:00')
      } else {
        this.$set(this.time, 0, getDatefmt(new Date(new Date().setDate(date.getDate() - 7)), 'yyyy-MM-dd') + ' 00:00:00')
      }
    },
    initData() {
      this.handleQuery()
    },
    handleQuery() {
      if (this.time != null) {
        this.query.alarmStartTime = this.time[0]
        this.query.alarmEndTime = this.time[1]
      } else {
        this.query.alarmStartTime = null
        this.query.alarmEndTime = null
      }
      this.tableLoading = true
      getList(this.query).then(res => {
        this.tableData = res.data.rows
        this.query.total = res.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 导出
    handleExport() {
      this.exportLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      download(query).then(result => {
        downloadFile(result, '历史报警信息', 'xlsx')
        this.exportLoading = false
      }).catch(() => {
        this.exportLoading = false
      })
    },
    // 点击树
    handleNodeClick(data) {
      console.log(data)
      this.handleReset()
      if (data.level === 1) {
        this.query.projectDepartmentId = data.id
      } else if (data.level === 2) {
        this.query.enterpriseId = data.id
      } else if (data.level === 3) {
        this.query.outletId = data.id
      }
      this.handleQuery()
    },
    // 点击排口数时所要重置的条件
    handleReset() {
      this.query = {
        total: 0,
        page: 1,
        limit: 10,
        projectDepartmentId: null,
        enterpriseId: null,
        outletId: null
      }
    },
    handleVerify(data) {
      this.dialogVisible = true
      this.obj = data
    },
    closeDialog() {
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
